<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" th:href="@{/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <!--使用Font Awesome需要引入-->
    <link rel="stylesheet" th:href="@{/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
    <script th:src="@{/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
    <script th:src="@{/js/lay-config.js?v=1.0.4}" charset="utf-8"></script>
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold" style="margin-left:10px!important;">全部折叠</button>
                <button type="button" id="addMenu" class="layui-btn layui-btn-danger" style="margin-left:10px!important;">新增</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script th:inline="none">
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: -1,
            treeIdName: 'authorityId',
            treePidName: 'parentId',
            elem: '#munu-table',
            async:false,
            // url: '../api/menus.json',
            url:"/menu/manage",
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'authorityName', minWidth: 200, title: '权限名称'},
                {field: 'menuUrl', title: '菜单url'},
                {
                    field: 'menuIcon', title: '图标', templet: function (d) {
                        return ' <i class="' + d.menuIcon + '"></i> ';
                    }
                },
                {
                    field: 'status', width: 80, align: 'center', title: '状态', templet: function (d) {
                        if (d.status === 1) {
                            return '<input checked="checked" lay-filter="switchTest" lay-skin="switch" lay-text="启用|停用" type="checkbox" disabled="disabled">';
                        } else {
                            return '<input  lay-filter="switchTest" lay-skin="switch" lay-text="启用|停用" type="checkbox" disabled="disabled">';
                        }
                    }
                },

                {
                    field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                        if (d.isMenu === 0) {
                            return '<span class="layui-badge layui-bg-orange">模块</span>';
                        } else if (d.isMenu === 1) {
                            return '<span class="layui-badge layui-bg-blue">导航</span>';
                        } else if (d.isMenu === 2) {
                            return '<span class="layui-badge layui-bg-green">功能</span>';
                        }else{
                            return '<span></span>';
                        }
                    }, title: '类型'
                },
                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            switch (obj.event) {
                case 'del':
                    var alert = layer.alert("确定删除？",{
                        title:"提示"
                    },function (){
                        var title =data.authorityName;
                        $.ajax({
                            url: '/menu/deleteMenu?title='+title,
                            type: "POST",
                            success: function (data) {
                                if (data.code == 200) {
                                    layer.alert(data.message,{
                                        title: "提示",
                                    },function (){
                                        // 关闭弹出层
                                        layer.close(alert);
                                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                                        parent.layer.close(iframeIndex);
                                        window.parent.location.reload();
                                    })
                                }else{
                                    layer.alert(data.message,{
                                        title: "提示",
                                    },function (){
                                        // 关闭弹出层
                                        layer.close(alert);
                                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                                        parent.layer.close(iframeIndex);
                                        window.parent.location.reload();
                                    })
                                }
                            }
                        })
                    })
                    break;
                case 'edit':
                    var authorityName = data.authorityName;
                    layer.open({
                        itle: '修改',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['800px', '600px'],
                        content: '/menuAudit?authorityName='+authorityName,
                    })
                    break;
            }
        });

        $("#addMenu").click(function (){
            layer.open({
                itle: '新增',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['800px', '600px'],
                content: '/menuAdd'
            })
        })
    });
</script>
</body>
</html>